package com.sunwayworld.basemodule.basedata.pageconfig.resource.impl;

import com.sunwayworld.basemodule.basedata.pageconfig.bean.LimsPageContentConfigBean;
import com.sunwayworld.basemodule.basedata.pageconfig.resource.LimsPageContentConfigOpenResource;
import com.sunwayworld.basemodule.basedata.pageconfig.service.LimsPageContentConfigService;
import com.sunwayworld.framework.database.sql.Order;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.module.item.file.bean.CoreFileBean;
import com.sunwayworld.module.item.file.manager.CoreFileManager;
import com.sunwayworld.module.item.file.service.CoreFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName LimsPageContentConfigOpenResourceImpl
 * @Author zhoujq
 * @Date 2023/8/22
 * @Version 1.0
 **/
@RestController
@GikamBean
public class LimsPageContentConfigOpenResourceImpl implements LimsPageContentConfigOpenResource {

    @Autowired
    @Lazy
    LimsPageContentConfigService limsPageContentConfigService;

    @Autowired
    @Lazy
    CoreFileService coreFileService;

    @Autowired
    @Lazy
    CoreFileManager coreFileManager;

    @Override
    public LimsPageContentConfigBean getConfig(Long id) {
        LimsPageContentConfigBean bean = limsPageContentConfigService.getDao().selectByIdIfPresent(id);
        if (bean != null) {
            CoreFileBean fileBean = coreFileService.selectFirstByFilter(SearchFilter.instance().match("targetId", limsPageContentConfigService.getDao().getTable().concat("$").concat(String.valueOf(id))).filter(MatchPattern.EQ).match("bizCategory", "LOGOPIC").filter(MatchPattern.EQ), Order.desc("id"));
            CoreFileBean fileBean2 = coreFileService.selectFirstByFilter(SearchFilter.instance().match("targetId", limsPageContentConfigService.getDao().getTable().concat("$").concat(String.valueOf(id))).filter(MatchPattern.EQ).match("bizCategory", "LOGOPIC_MAIN").filter(MatchPattern.EQ), Order.desc("id"));
            if (fileBean != null) {
                bean.setLogoUrl(coreFileManager.getDownloadUrl(fileBean));
            }
            if (fileBean2 != null) {
                bean.setLogoUrlMain(coreFileManager.getDownloadUrl(fileBean2));
            }
            return bean;
        }
        return null;
    }
}
